<!-- 提货专区商品详情 -->
<template>
	<view class="container">
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<block v-for="(item,index) in storeInfo.slider_image" :key="index">
					<swiper-item class="swiper-item">
						<view class="image-wrapper">
							<easy-loadimage mode="widthFix" :scroll-top="scrollTop" :image-src="item"></easy-loadimage>
						</view>
					</swiper-item>
				</block>
			</swiper>
			<view class="cardInfoMin">
				<text class="a">{{cardInfo.name||''}}</text>
				<view class="b">
					<text class="b-a" v-if="cardInfo.card_value">{{cardInfo.card_value||''}}</text>
					<text class="b-a no" v-else>提货卡</text>
					<text class="b-b yticon icon-you" v-if="!cardInfo.card_value"></text>
				</view>
			</view>
		</view>

		<view class="introduce-section">
			<view class="pd-30">
				<view class="title">
					{{storeInfo.store_name}}
				</view>
				<view class="price-box">
					<text class="price">{{goodsPrice}}</text>
				</view>
			</view>
		</view>
		<view class="c-list">
			<view class="c-row" @click="toggleSpec(3)">
				<text class="tit">规格</text>
				<view class="con">
					<text class="selected-text" v-if="selectDisObj.suk">{{selectDisObj.suk}}</text>
					<text class="selected-text no-select" v-else>请选择规格</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
		</view>
		<view class="cut"></view>
		<view class="detail-desc">
			<view class="txt">商品详情</view>
			<rich-text class="rich-text" :nodes="description"></rich-text>
		</view>

		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/mall/pickCardMall/pickCardCart" class="p-b-btn">
				<image lazy-load src="../static/images/tubiao.png" mode=""></image>
				<text>提货栏</text>
			</navigator>
			<view class="action-btn-group">
				<view class="action-btn no-border buy-now-btn" @click="toggleSpec(2)">加入提货栏</view>
				<view class="action-btn no-border add-cart-btn" @tap="toggleSpec(1)">立即提货</view>
			</view>
		</view>
		<!-- 规格-模态层弹窗 -->
		<view class="popup spec" :class="specClass" @touchmove.stop.prevent="stopPrevent" @click="toggleSpec">
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content" @click.stop="stopPrevent">
				<view>
					<view class="a-t">
						<image lazy-load :src="selectDisObj.image"></image>
						<view class="right">
							<text class="price">{{selectDisObj.price}}</text>
							<text class="stock">库存：{{selectDisObj.stock}}件</text>
							<view class="selected" v-if="selectDisObj.suk">
								已选：{{selectDisObj.suk}}
							</view>
						</view>
					</view>
					<view class="a-c">
						<view class="attr-list">
							<block v-for="(items, index) in specList" :key="index">
								<text class="specTitle">{{items.attr_name}}</text>
								<view class="itemBox">
									<view @tap='selectSpec(item,index,idx)' :class="subIndex[index] == idx ?'itemAct':''" class="item" v-for="(item,idx) in items.attr_value"
									 :key="idx">{{item.attr}}</view>
								</view>
							</block>
						</view>
					</view>
					<view class="num">
						<text>数量</text>
						<uni-number-box class="uni-number-box" :min="1" :value="goodsNum" @eventChange="numberChange"></uni-number-box>
					</view>
					<view class="btn-box">
						<button class="btn" @click="submit">{{btnType == 1 ? '立即提货' : '确认'}}</button>
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import uniNumberBox from '@/components/uni-number-box/uni-number-box.vue'
	import utils from "@/common/utils.js"
	import {
		systemConfig
	} from '@/api/public.js'
	import {
		takeProductsDetail,
		takeCartAdd,
	} from '@/api/mall.js'
	import {
		formatRichText
	} from '@/utils/util.js'
	import {
		deliveryCard
	} from '@/api/user.js'
	export default {
		components: {
			uniNumberBox
		},
		data() {
			return {
				type: 0, //是否从商城首页跳转 是/1 否/0
				cardInfo: {}, //提货卡信息
				storeInfo: {}, //商品信息
				slider_image: [], //轮播图
				specClass: 'none', //规格弹窗开关
				specList: [], //规格列表
				selectArr: [], // 选择的规格
				selectDisObj: { //选择的规格对应产品信息
					price: 0,
				},
				goodsNum: 1, //选择的商品数量
				sku: '', //选中的规格索引
				unique: null, //选中规格标识
				btnType: 0,
				description: '', //商品详情
				seller_info: {
					seller_type: 1,
				}, //商家信息
				reply: {}, //评价信息
				data: {},
				subIndex: [], //选中规格索引
				goodsPrice: 0, //商品价格
				scrollTop: 0
			};
		},
		onLoad(options) {
			if (uni.getStorageSync("cardId") != 0) {
				this.type = 0
			} else {
				this.type = 1
			}
			takeProductsDetail({
				id: options.id
			}).then(res => {
				this.data = res.data
				this.storeInfo = res.data.storeInfo
				this.reply = res.data.reply
				this.slider_image = res.data.storeInfo.slider_image
				if (res.data.productAttr.length !== 0) {
					this.specList = res.data.productAttr
				}
				this.goodsPrice = res.data.storeInfo.price
				this.selectDisObj = res.data.storeInfo
				this.description = formatRichText(res.data.storeInfo.description)
			})
		},
		onShow() {
			deliveryCard({
				type: this.type,
				card_id: uni.getStorageSync("cardId"),
				order_id: uni.getStorageSync("card_order_id")||''
			}).then(res => {
				if (res.data.cardInfo.name) {
					this.cardInfo = res.data.cardInfo
					this.cardInfo.card_value = utils.format(this.cardInfo.card_value)
				} else {
					this.cardInfo.name = "前往购买"
				}
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		methods: {
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			numberChange(e) {
				this.goodsNum = e.number;
			},
			//规格弹窗开关
			toggleSpec(type) {
				this.btnType = type
				if (this.specClass === 'show') {
					this.specClass = 'hide'
				} else if (this.specClass === 'hide' || this.specClass === 'none') {
					this.specClass = 'show'
				}
			},
			//选择规格
			selectSpec(item, index, idx) {
				if (this.selectArr[index] !== item.attr) {
					this.$set(this.selectArr, index, item.attr)
					this.$set(this.subIndex, index, idx)
					this.sku = String(this.selectArr)
					if (this.data.productValue[this.sku]) {
						this.selectDisObj = this.data.productValue[this.sku]
					}
				}
			},
			toOnlineStore() {
				// this.seller_info.seller_id
				uni.navigateTo({
					url: `/mall/product/onlineStore?id=${this.seller_info.seller_id}`
				})
			},
			callStore() {
				uni.makePhoneCall({
					phoneNumber: this.seller_info.online_phone
				})
			},
			submit() {
				if(!uni.getStorageSync("card_order_id")) {
					return uni.showToast({
						icon:"none",
						title: "请先购买提货卡"
					})
				}
				uni.showLoading({mask:true});
				if (this.btnType == 1) { //买
					takeCartAdd({
						productId: this.storeInfo.id,
						cartNum: this.goodsNum,
						uniqueId: this.selectDisObj.unique,
						new: 1, //是否购物车购买|0=购物车购买,1=直接购买
					}).then(res => {
						uni.hideLoading()
						uni.navigateTo({
							url: `/mall/pickCardMall/pickCardCreateOrder?cartId=${res.data.cartId}`
						})
					})
				} else if (this.btnType == 2) { //加购
					takeCartAdd({
						productId: this.storeInfo.id,
						cartNum: this.goodsNum,
						uniqueId: this.selectDisObj.unique,
						new: 0,
					}).then(res => {
						uni.hideLoading()
						uni.showToast({
							icon: "none",
							title: "加入提货栏成功！"
						})
						this.specClass = 'hide'
					})
				} else if (this.btnType == 3) { //选择确定
					uni.hideLoading()
					this.specClass = 'hide'
				}
			},
			stopPrevent() {},
			previewPic(urls, index) {
				uni.previewImage({
					urls: urls,
					current: index
				})
			},

		},
		onHide() {
			this.specClass = 'none'
		},
	}
</script>

<style lang='scss' scoped>
	.container {
		overflow-x: hidden;
	}

	page {
		overflow-x: hidden;
		background: $page-color-base;
		padding-bottom: 160rpx;
	}

	.cut {
		width: 100%;
		height: 20rpx;
		background: #f5f5f5;
	}

	.icon-you {
		font-size: $uni-font-size-base + 1rpx;
		color: #999;
	}

	.carousel {
		height: 722rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			height: 100%;
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.cardInfoMin {
			position: absolute;
			left: 0;
			bottom: 20rpx;
			padding: 0rpx 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			height: 90rpx;
			background: #FFF9E2;
			border-radius: 0px 8rpx 8rpx 0px;
			z-index: 99;

			.a {
				font-size: 20rpx;
				font-weight: 500;
				color: rgba(51, 51, 51, 1);
			}

			.b {
				display: flex;
				align-items: center;

				.b-a {
					font-size: 24rpx;
					font-weight: bold;
					color: #FAD03C;
				}

				.b-b {
					margin-left: 4rpx;
					width: 20rpx;
					height: 20rpx;
					line-height: 20rpx;
					text-align: center;
					background: #FEC93B;
					border-radius: 50%;
					color: #fff;
					font-size: 14rpx;
				}

				.no {
					font-size: 20rpx;
					font-weight: normal;
					color: rgba(51, 51, 51, 1);
				}

			}
		}

	}

	/* 标题简介 */
	.introduce-section {
		background: #fff;

		.pd-30 {
			padding: 20rpx 30rpx 0;

			image {
				width: 70rpx;
				height: 28rpx;
				margin-right: 12rpx;
			}
		}

		.title {
			font-weight: 500;
			color: #333333;
			font-size: 32rpx;
			/* height: 50rpx; */
			line-height: 50rpx;
			text-align: justify;

			.lebal {
				padding: 5rpx 15rpx;
				font-size: 20rpx;
				text-align: center;
				color: #fff;
				background: rgba(253, 117, 103, 1);
				border-radius: 50px;
				margin-right: 10rpx;
			}
		}

		.black_rate {
			height: 24rpx;
			line-height: 24rpx;
			border: 1px solid rgba(255, 31, 47, 1);
			border-radius: 6rpx;
			font-size: 18rpx;
			color: rgba(255, 31, 47, 1);
			display: inline-block;
			padding: 3rpx 10rpx;
		}

		.price-box {
			display: flex;
			align-items: baseline;
			height: 64rpx;
			padding: 10rpx 0 20rpx;
			border-bottom: 1rpx #f4f4f4 solid;
			font-size: 26rpx;
			color: $mall-price;

			.price {
				font-size: 40rpx;
				font-weight: bold;

				&:after {
					content: " 点";
					font-size: 24rpx;
				}
			}
		}

		.bot-row {
			display: flex;
			align-items: center;
			height: 50rpx;
			font-size: 20rpx;
			color: #B4B4B4;
			justify-content: space-between;
			padding-bottom: 25rpx;

			text {
				/* flex: 1;
				text-align: center; */
			}
		}
	}

	.c-list {
		font-size: $uni-font-size-sm + 2rpx;
		color: $mall-color-base;
		background: #fff;

		.c-row {
			display: flex;
			align-items: center;
			padding: 30rpx;
			position: relative;
		}

		.b-b {
			border-top: 1px solid #E5E5E5;
		}

		.tit {
			font-size: 28rpx;
			color: #999;
			width: 140rpx;
		}

		.con {
			flex: 1;
			padding-right: 56rpx;
			font-size: 24rpx;
			text-align: right;
			color: $font-color-dark;

			.selected-text {
				color: #999;
				padding: 8rpx 18rpx;
				border-radius: 50rpx;
				border: 2rpx solid rgba(237, 237, 237, 1);
				margin-left: 10rpx;
			}

			.no-select {
				border: none;
			}
		}
	}

	/* 评价 */
	.eva-section {
		display: flex;
		flex-direction: column;
		padding: 30rpx;
		background: #fff;

		.e-header {
			display: flex;
			align-items: center;
			font-size: $uni-font-size-sm - 2rpx;
			color: $font-color-light;

			.tit {
				font-size: 28rpx;
				color: #333333;
				margin-right: 4rpx;
			}

			.desc {
				font-size: 22rpx;
				color: #666666;

			}

			.tip {
				flex: 1;
				text-align: right;
			}

			.icon-you {
				color: #999;
				margin-left: 10rpx;
			}
		}
	}

	.eva-box {
		display: flex;
		padding: 20rpx 0;

		.portrait {
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 100px;
		}

		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $uni-font-size-base;
			color: $mall-color-base;
			padding-left: 26rpx;

			.con {
				font-size: $uni-font-size-base;
				color: $font-color-dark;
				padding: 20rpx 0;
			}

			.bot {
				display: flex;
				justify-content: space-between;
				font-size: $uni-font-size-sm;
				color: $font-color-light;
			}
		}

		.comment-pics {
			width: 100%;
			display: flex;
			justify-content: space-between;
			padding: 30rpx 0;

			image {
				width: 33%;
				height: 200rpx;
				border-radius: 10rpx;

			}
		}
	}

	.more-comm {
		width: 180rpx;
		margin: 20rpx auto;
		line-height: 60rpx;
		height: 60rpx;
		border: 1px solid rgba(153, 153, 153, 1);
		border-radius: 30rpx;
		font-size: 20rpx;
		color: rgba(51, 51, 51, 1);
		text-align: center;

	}

	.shop {
		margin: 40rpx 32rpx;
		display: flex;
		position: relative;

		image {
			width: 120rpx;
			height: 120rpx;
			border-radius: 8rpx;
		}

		.infoBox {
			display: flex;
			flex-direction: column;
			justify-content: center;
			margin-left: 25rpx;

			.title {
				font-size: 28rpx;
				font-weight: bold;
				color: rgba(51, 51, 51, 1);
				margin-bottom: 28rpx;
			}

			.info__rate {
				font-size: 24rpx;
				color: rgba(51, 51, 51, 1);
			}

			.rate {
				margin-left: 12rpx;
			}
		}

		.goto {
			position: absolute;
			top: 30%;
			right: 12rpx;
			font-size: 24rpx;
			color: #333333;

			.name {
				padding-right: 10rpx;
			}
		}
	}

	/*  详情 */
	.detail-desc {
		background: #fff;
		margin-top: 16rpx;
		padding-bottom: 140rpx;

		.txt {
			padding: 40rpx 0rpx;
			text-align: center;
			font-size: 30rpx;
			color: #999;
			background: #fff;
		}

		.rich-text {
			width: 100%;
			overflow-x: hidden;

			.img {
				width: 100%;
			}

			image {
				width: 100%;
			}
		}
	}

	/* 规格选择弹窗 */
	.attr-content {
		padding: 30rpx;
		box-sizing: border-box;

		.a-c {
			position: absolute;
			top: 150rpx;
			bottom: 210rpx;
			overflow-y: scroll;
		}

		.a-t {
			display: flex;

			image {
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -70rpx;
				border-radius: 8rpx;
			}

			.right {
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: 20rpx;
				color: #999;
				line-height: 30rpx;

				.price {
					line-height: 40rpx;
					font-weight: bold;
					font-size: $uni-font-size-lg;
					color: $mall-price;

					&:after {
						content: ' 点';
						font-size: 20rpx;
					}
				}
			}
		}

		.num {
			position: absolute;
			left: 0;
			bottom: 120rpx;
			height: 100rpx;
			width: 100%;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.uni-number-box {
				width: 200rpx;
				height: 50rpx;
				position: relative;
			}
		}

		.btn-box {
			position: absolute;
			left: 0;
			bottom: 0;
			width: 100%;
			border-top: 1px solid #eee;
			z-index: 99;
			height: 119rpx;
			display: flex;
			align-items: center;

			.btn {
				height: 76rpx;
				line-height: 76rpx;
				border-radius: 38rpx;
				background: $mall-price;
				font-size: 28rpx;
				color: #fff;
				width: 690rpx;
			}
		}

		.attr-list {
			padding-top: 30rpx;

			.specTitle {
				font-size: 28rpx;
				color: #999;
			}

			.itemBox {
				margin-top: 30rpx;
				display: flex;
				flex-wrap: wrap;

				.item {
					font-size: 24rpx;
					height: 50rpx;
					line-height: 50rpx;
					text-align: center;
					color: #999;
					border: 1rpx solid #999;
					margin-right: 30rpx;
					padding: 0 30rpx;
					border-radius: 25rpx;
					margin-bottom: 30rpx;
				}

				.itemAct {
					background-color: $mall-price;
					border: 1rpx solid $mall-price;
					color: #fff;
				}
			}
		}

	}

	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;

		&.show {
			display: block;

			.mask {
				animation: showPopup 0.2s linear both;
			}

			.layer {
				animation: showLayer 0.2s linear both;
			}
		}

		&.hide {
			animation: hideLayer 0.2s linear both;

			.mask {
				animation: hideLayer 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}

		&.none {
			display: none;
		}

		.mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}

		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			padding-bottom: env(safe-area-inset-bottom);
			width: 100%;
			height: 70vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;
		}

		@keyframes showPopup {
			0% {
				opacity: 0;
			}

			100% {
				opacity: 1;
			}
		}

		@keyframes hidePopup {
			0% {
				opacity: 1;
			}

			100% {
				opacity: 0;
			}
		}

		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}

			100% {
				transform: translateY(0%);
			}
		}

		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}

			100% {
				transform: translateY(120%);
			}
		}
	}

	/* 底部操作菜单 */
	.page-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		padding: 30rpx 0 30rpx;
		padding: 30rpx 0 calc(30rpx + env(safe-area-inset-bottom));
		background: #fff;
		box-shadow: -1rpx -5rpx 4rpx 0rpx rgba(51, 51, 51, 0.05);

		.p-b-btn {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 24rpx;
			color: rgba(51, 51, 51, 1);
			width: 140rpx;
			height: 80rpx;
			margin-right: 10rpxs;

			image {
				width: 45rpx;
				height: 40rpx;
				margin-bottom: 10rpx;
			}
		}

		.action-btn-group {
			color: #333333;
			display: flex;
			height: 80rpx;
			overflow: hidden;
			margin-left: 20rpx;

			.action-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 250rpx;
				height: 100%;
				font-size: 32rpx;
				font-weight: 500;
				padding: 0;
				border-radius: 40rpx;
				background: transparent;
			}

			.buy-now-btn {
				background: linear-gradient(90deg, #FBCC2B 0%, #FBDC72 100%);
			}

			.add-cart-btn {
				margin-left: 30rpx;
				background: linear-gradient(90deg, #FD895A 0%, #FFA886 100%);
			}
		}
	}
</style>
